package com.sls.web.manager.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.socket.SuperService;
import com.sls.web.manager.sys.pojo.Permission;
import com.sls.web.manager.sys.querybean.MenuQuery;
import com.sls.web.manager.sys.service.MenuBuildService;
import com.whalin.MemCached.MemCachedClient;

@SuppressWarnings("unchecked")
@Service("menuBuildService")
public class MenuBuildServiceImpl extends SuperService implements MenuBuildService {
	private static Logger log = Logger.getLogger(MenuBuildService.class);
	@Resource(name = "memcachedClient")
	private MemCachedClient memcachedClient;

	public final boolean builMenu() {
		List<Permission> list1 = build(Constants.BUILD_MENU1);
		Map<String, Permission> map = new HashMap<String, Permission>();
		if (list1 != null) {
			for (Permission p : list1) {
				map.put(p.getCode(), p);
			}
		}
		boolean b = memcachedClient.set(Constants.MENU1, map);
		log.info("加载一级菜单:" + b);
		
		b = memcachedClient.set(Constants.MENU2, build(Constants.BUILD_MENU2));
		log.info("加载二级菜单:" + b);
		
		b = memcachedClient.set(Constants.MENU3, build(Constants.BUILD_MENU3));
		log.info("加载三级菜单:" + b);
		
		b = memcachedClient.set(Constants.NOTMENU, build(Constants.BUILD_MENU));
		log.info("加载所有非菜单的地址:" + b);
		rightsMenu();
		return b;
	}

	public final boolean rightsMenu() {
		Map<String, String> menuMap = new HashMap<String, String>();
		Object menu = memcachedClient.get(Constants.MENU1);
		// 没有数据的时候重新获取数据库表的菜单
		if (menu == null)
			this.builMenu();
		if (menu != null && menu instanceof HashMap) {
			Map<String, Permission> map = (HashMap<String, Permission>) menu;
			Iterator<Entry<String, Permission>> st = map.entrySet().iterator();
			while (st.hasNext()) {
				Entry<String, Permission> entry = st.next();
				Permission permission = entry.getValue();
				memcachedClient.set(Constants.MENU_MEM_TIP + permission.getUrl(), permission.getCode());
			}
		}
		log.info("加载一级菜单:" + menu);
		menu = memcachedClient.get(Constants.MENU2);
		if (menu != null && menu instanceof ArrayList) {
			List<Permission> list = (ArrayList<Permission>) menu;
			Iterator<Permission> st = list.iterator();
			while (st.hasNext()) {
				Permission permission = st.next();
				memcachedClient.set(Constants.MENU_MEM_TIP + permission.getUrl(), permission.getCode());
			}
		}
		log.info("加载二级菜单:" + menu);
		menu = memcachedClient.get(Constants.MENU3);
		if (menu != null && menu instanceof ArrayList) {
			List<Permission> list = (ArrayList<Permission>) menu;
			Iterator<Permission> st = list.iterator();
			while (st.hasNext()) {
				Permission permission = st.next();
				memcachedClient.set(Constants.MENU_MEM_TIP + permission.getUrl(), permission.getCode());
			}
		}
		log.info("加载三级菜单:" + menu);
		menu = memcachedClient.get(Constants.NOTMENU);
		if (menu != null && menu instanceof ArrayList) {
			List<Permission> list = (ArrayList<Permission>) menu;
			Iterator<Permission> st = list.iterator();
			while (st.hasNext()) {
				Permission permission = st.next();
				memcachedClient.set(Constants.MENU_MEM_TIP + permission.getUrl(), permission.getCode());
			}
		}
		log.info("加载非菜单项:" + menu);
		return true;
	}

	@Override
	public final String getCode(String url) {
		url = url.trim();
		Object ob = this.memcachedClient.get(Constants.MENU_MEM_TIP + url);
		if (ob != null)
			return ob.toString();
		return null;
	}

	@Override
	public List<Permission> getAllMenu() {
		Map<String, Permission> map = (Map<String, Permission>) memcachedClient.get(Constants.MENU1);
		List<Permission> list1 = new ArrayList<Permission>();
		list1.addAll(map.values());
		List<Permission> list2 = (List<Permission>) memcachedClient.get(Constants.MENU2);
		List<Permission> list3 = (List<Permission>) memcachedClient.get(Constants.MENU3);

		List<Permission> all = new ArrayList<Permission>();
		for (Permission p1 : list1) {
			all.add(p1);
			for (Permission p2 : list2) {
				if (p1.getCode().equals(p2.getParentCode())) {
					all.add(p2);
					for (Permission p3 : list3) {
						if (p2.getCode().equals(p3.getParentCode())) {
							all.add(p3);
						}
					}
				}
			}
		}
		return all;
	}

	@Override
	public List<Permission> getAllNOtMenu() {
		List<Permission> list4 = (List<Permission>) memcachedClient.get(Constants.NOTMENU);
		return list4;
	}

	private List<Permission> build(String str) {
		int offset = 0;
		int limit = 50;
		Gson gson = CommonUtil.gson();
		SocketDTO DTO = new SocketDTO();
		MenuQuery query = new MenuQuery(offset,limit);
		Head head = new Head();
		head.setIp("locahost");
		head.setUserId("system");
		head.setServiceCode(str);
		List<Permission> list1 = new ArrayList<Permission>();
		List<Permission> list=null;
		do {
			DTO=send(head,query);
			query.setOffset(limit * (++offset));
			list = gson.fromJson(DTO.getBody(), new TypeToken<List<Permission>>() {
			}.getType());
			if(list!=null)
			list1.addAll(list);
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		} while (list != null && list.size() >= limit);

		return list1;
	}
}
